//
// Created by syj on 2024/3/14.
//

#ifndef TRANSMIDDLE_REMOTE_SINK_H
#define TRANSMIDDLE_REMOTE_SINK_H

#include <spdlog/common.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/sinks/base_sink.h>

#include <mutex>
#include <set>
#include <string>

//#include "middle/app/module.h"
//#include "middle/core/string.h"
#include "middle/log/common/common.h"
#include "middle/log/service/log_config.h"

namespace log
{
    template <typename Mutex>
    class remote_sink final : public spdlog::sinks::base_sink<Mutex> {
    public:
        explicit remote_sink(log::LogConfig const &confg, std::promise<bool> &promise);
    protected:
        void sink_it_(const spdlog::details::log_msg &msg) override;
        void flush_() override;

    private:
        log::LogConfig config_;
        Logger logger_;
        std::promise<bool> read_;
    };

    using remote_sink_mt = remote_sink<std::mutex>;
    using remote_sink_st = remote_sink<spdlog::details::null_mutex>;

    template <typename Factory = spdlog::synchronous_factory>
    inline std::shared_ptr<spdlog::logger> remote_logger_mt(log::LogConfig &confg) {
        return Factory::template create<remote_sink_mt>(confg);
    }

    template <typename Factory = spdlog::synchronous_factory>
    inline std::shared_ptr<spdlog::logger> remote_logger_st(log::LogConfig &confg) {
        return Factory::template create<remote_sink_st>(confg);
    }

}//namespace log

#endif //TRANSMIDDLE_REMOTE_SINK_H
